package com.groupseven.backend.controller;

import com.groupseven.backend.configuration.WebSocket.WebSocket;
import com.groupseven.backend.domain.Paper.Paper;
import com.groupseven.backend.domain.Patent.Patent;
import com.groupseven.backend.domain.PrizeProduct.PrizeProduct;
import com.groupseven.backend.domain.Program.Program;
import com.groupseven.backend.domain.Program.ProgramApplication;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.services.*;
import com.groupseven.backend.test.DataEncode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/audit")
public class ProgramAduitController {
    @Autowired
    private ProgramAduitService programAduitService;
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private DataEncode dataEncode;
    @Autowired
    private PaperService paperService;
    @Autowired
    private ProgramService programService;
    @Autowired
    private PatentService patentService;
    @Autowired
    private PrizeProductService prizeProductService;

    @GetMapping("/programs/")
    public ResultType selectprogramsByApprovalStatus (){
        List<Program> programs=programAduitService.selectprogramsByApprovalStatus();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPrograms(programs);
        return resultType;
    }
    @GetMapping("/papers/")
    public ResultType selectpapersByApprovalStatus (){
        List<Paper> papers=programAduitService.selectpapersByApprovalStatus();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPapers(papers);
        return resultType;
    }
    @GetMapping("/patents/")
    public ResultType selectpatentsByApprovalStatus (){
        List<Patent> patents=programAduitService.selectpatentsByApprovalStatus();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPatents(patents);
        return resultType;
    }
    @GetMapping("/prizeproduct/")
    public ResultType selectprizeproductsByApprovalStatus (){
        List<PrizeProduct> prizeProducts=programAduitService.selectprizeproductsByApprovalStatus();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPrizeProducts(prizeProducts);
        return resultType;
    }
    @GetMapping("/programApplication/")
    public ResultType selectprogramApplicationByApprovalStatus(){
        List<ProgramApplication> programApplications=programAduitService.selectprogramApplicationByApprovalStatus();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setApplications(programApplications);
        return resultType;
    }


    @GetMapping("/programs/getall")
    public ResultType selectprograms(){
        List<Program> programs=programAduitService.selectprograms();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPrograms(programs);
        return resultType;
    }
    @GetMapping("/papers/getall")
    public ResultType selectpapers(){
        List<Paper> papers=programAduitService.selectpapers();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPapers(papers);
        return resultType;
    }
    @GetMapping("/patents/getall")
    public ResultType selectpatents(){
        List<Patent> patents=programAduitService.selectpatents();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPatents(patents);
        return resultType;
    }
    @GetMapping("/prizeproduct/getall")
    public ResultType selectprizeproducts(){
        List<PrizeProduct> prizeProducts=programAduitService.selectprizeproducts();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPrizeProducts(prizeProducts);
        return resultType;
    }
    @GetMapping("/programApplication/getall")
    public ResultType selectprogramApplication(){
        List<ProgramApplication> programApplications=programAduitService.selectprogramApplication();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setApplications(programApplications);
        return resultType;
    }


    @PutMapping("/programs/3/")
    public ResultType updateprogramsByApprovalStatus(@RequestBody Program program){
        programAduitService.updateprogramsByApprovalStatus(program);
        webSocket.sendOneMessage(program.getManager(), program.getManager()+"，你的项目【"+program.getProgramname()+"】未过审");
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/papers/3/")
    public ResultType updatepapersByApprovalStatus(@RequestBody Paper paper){
        programAduitService.updatepapersByApprovalStatus(paper);
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/patents/3/")
    public ResultType updatepatentsByApprovalStatus(@RequestBody Patent patent){
        programAduitService.updatepatentsByApprovalStatus(patent);
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/prizeproduct/3/")
    public ResultType updateprizeproductByApprovalStatus(@RequestBody PrizeProduct prizeProduct){
        programAduitService.updateprizeproductsByApprovalStatus(prizeProduct);
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/programApplication/3/")
    public ResultType updateprogramApplicationByApprovalStatus(@RequestBody ProgramApplication programApplication){
        programAduitService.updateprogramApplicationByApprovalStatus(programApplication);
        return new ResultType(1,"修改成功",200);
    }


    @PutMapping("/programs/4/")
    public ResultType updateNOprogramsByApprovalStatus(@RequestBody Program program){
        programAduitService.updateNOprogramsByApprovalStatus(program);
        webSocket.sendOneMessage(program.getManager(), program.getManager()+"，你的项目【"+program.getProgramname()+"】未过审");
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/papers/4/")
    public ResultType updateNOpapersByApprovalStatus(@RequestBody Paper paper){
        programAduitService.updateNOpapersByApprovalStatus(paper);
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/patents/4/")
    public ResultType updateNOpatentsByApprovalStatus(@RequestBody Patent patent){
        programAduitService.updateNOpatentsByApprovalStatus(patent);
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/prizeproduct/4/")
    public ResultType updateNOprizeproductByApprovalStatus(@RequestBody PrizeProduct prizeProduct){
        programAduitService.updateNOprizeproductsByApprovalStatus(prizeProduct);
        return new ResultType(1,"修改成功",200);
    }
    @PutMapping("/programApplication/4/")
    public ResultType updateNOprogramApplicationByApprovalStatus(@RequestBody ProgramApplication programApplication){
        programAduitService.updateNOprogramApplicationByApprovalStatus(programApplication);
        return new ResultType(1,"修改成功",200);
    }

    /**
     * 模糊查询论文
     * @Author 王兆阳
     * @return 论文
     */
    @GetMapping("/search/paper/{query}")
    public ResultType searchqueryPaper(@PathVariable String query){
        if(paperService.searchqueryPaper(query).isEmpty()){
            return new ResultType(0,"查询失败",200);
        }
        else {
            List<Paper> papers=paperService.searchqueryPaper(query);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(papers);
            return resultType;
        }
    }

    /**
     * 模糊查询项目
     * @Author 王兆阳
     * @return 项目
     */
    @GetMapping("/search/program/{query}")
    public ResultType searchPrograms(@PathVariable String query){
        List<Program> fuzzy = programService.FuzzySearch(query);
        ResultType result = new ResultType(1,"查询成功！",200);
        result.setPrograms(fuzzy);
        return result;
    }

    /**
     * 模糊查询专利
     * @Author 王兆阳
     * @return 专利
     */
    @GetMapping("/search/patent/{query}")
    public ResultType searchqueryPatent(@PathVariable String query){
        if(patentService.searchqueryPatent(query).isEmpty()){
            return new  ResultType(0,"查询失败,",200);
        }
        else {
            List<Patent> patents=patentService.searchqueryPatent(query);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPatents(patents);
            return resultType;
        }
    }

    /**
     * 模糊查询获奖项目
     * @Author 王兆阳
     * @return 获奖项目
     */
    @GetMapping("/search/award/{query}")
    public ResultType searchqueryPrizeProduct(@PathVariable String query){
        if(prizeProductService.searchqueryPrizeProduct(query).isEmpty()){
            return new  ResultType(0,"查询失败",200);
        }
        else
        {
            List<PrizeProduct> prizeProducts=prizeProductService.searchqueryPrizeProduct(query);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProducts(prizeProducts);
            return  resultType;
        }
    }
}


